﻿using System.Linq;
using System.Web.Mvc;

namespace Web7.Mvc.ViewEngines
{
    public class RazorViewEngineProvider : IViewEngineProvider
    {
        static readonly string[] DisabledFormats = new[] { "~/Disabled" };

        public IViewEngine CreateViewEngine(CreateViewEngineParams parameters)
        {
            var areaFormats = new[] {
                                        "~/Apps/{2}/Views/{1}/{0}.cshtml",
                                        "~/Continaer/{2}/Views/{1}/{0}.cshtml",
                                        "~/Views/{2}/Views/{1}/{0}.cshtml",
                                    };

            var universalFormats = parameters.VirtualPaths
                .SelectMany(x => new[] {
                                           x + "/Views/{0}.cshtml",
                                       })
                .ToArray();

            var viewEngine = new RazorViewEngine
            {
                MasterLocationFormats = DisabledFormats,
                ViewLocationFormats = universalFormats,
                PartialViewLocationFormats = universalFormats,
                AreaMasterLocationFormats = DisabledFormats,
                AreaViewLocationFormats = areaFormats,
                AreaPartialViewLocationFormats = areaFormats,
            };

            return viewEngine;
        }

        public IViewEngine CreateBareViewEngine()
        {
            return new RazorViewEngine
            {
                MasterLocationFormats = DisabledFormats,
                ViewLocationFormats = DisabledFormats,
                PartialViewLocationFormats = DisabledFormats,
                AreaMasterLocationFormats = DisabledFormats,
                AreaViewLocationFormats = DisabledFormats,
                AreaPartialViewLocationFormats = DisabledFormats,
            };
        }
    }
}
